<?php
class BusinessController extends Controller {
	
	/**
	 * 业务派工
	 * Enter description here ...
	 */
	function DispatchingAction(){
		$b["cid"]=$this->user["cid"];
		$types = BusinessModel::getInstance()->StaffBybusiness($b);
		if(isset($types) && $types != ""){
			$this->JS["types"] = $types;
		}else{
			$this->JS["types"] = " ";
		}
		$this->view("business.dispatching","item");
	}

	/**
	 * 数据列表
	 * Enter description here ...
	 */
	function DispatchingListAction(){
		$p["p"] = P("pageIndex",0);
		$p["r"] = P("pageSize",20);
		$p["sortField"] = P("sortField","id");
		$p["sortOrder"] = P("sortOrder","asc");
		$p['id']=P("id","0");
		$p['types']=P("types","0");
		$p['dis_agency_id']=P("dis_agency_id",$this->user["agency_id"]);
		$p['con_agency_id']=P("con_agency_id",$this->user["agency_id"]);
		$p['created_start']=P("created_start","");
		$p['created_end']=P("created_end","");
		$p['order_no']=P("order_no","0");
		$p['order_id']=P("order_id","");
		$p['statuss']=P("statuss","0");
		$agency = CommonModel::getInstance()->getAgencyAllList();
		$p['dis_agency_id'] = ArrayTool::ArrayToIDs($agency,$p['dis_agency_id']);
		$p['con_agency_id'] = ArrayTool::ArrayToIDs($agency,$p['con_agency_id']);
		$data = BusinessModel::getInstance()->getDispatchingList($p);
		$this->json->toJson(array("total"=>BusinessModel::getInstance()->getCount($p),"data"=>$data));
	}
	
    /**
	 * 数据列表
	 * Enter description here ...
	 */
	function ConDispatchingListAction(){
		$p["p"] = P("pageIndex",0);
		$p["r"] = P("pageSize",20);
		$p["sortField"] = P("sortField","id");
		$p["sortOrder"] = P("sortOrder","asc");
		$p['id']=P("id","0");
		$p['order_id']=P("order_id","");
		$p['types']=P("types","0");
		$p['con_agency_id']=P("con_agency_id",$this->user["agency_id"]);
		$agency = CommonModel::getInstance()->getAgencyAllList();
		$p['con_agency_id'] = ArrayTool::ArrayToIDs($agency,$p['con_agency_id']);
		
		$data = BusinessModel::getInstance()->getConDispatchingList($p);
		$this->json->toJson(array("total"=>BusinessModel::getInstance()->getConCount($p),"data"=>$data));
	}
	
	/**
	 * 工单审核
	 * Enter description here ...
	 */
	function DispatchingReviewAction(){
		$p["p"] = P("pageIndex",0);
		$p["r"] = P("pageSize",20);
		$p["sortField"] = P("sortField","id");
		$p["sortOrder"] = P("sortOrder","asc");
		$p['id']=P("id","0");
		$p['order_id']=P("order_id","");
		$p['types']=P("types","0");
		$p['con_agency_id']=P("con_agency_id",$this->user["agency_id"]);
		$agency = CommonModel::getInstance()->getAgencyAllList();
		$p['con_agency_id'] = ArrayTool::ArrayToIDs($agency,$p['con_agency_id']);
		
		$data = BusinessModel::getInstance()->getDispatchingReview($p);
		$this->json->toJson(array("total"=>BusinessModel::getInstance()->getReviewCount($p),"data"=>$data));
	}
	
	/**
	 * 派工到下级机构
	 * Enter description here ...
	 */
	function SetAgencyAction(){
		$p["uid"]=$this->user["id"];
		$this->data["StaffInfo"]=CommonModel::getInstance()->getStaffInfo($p);
		$this->view("business.setagency.edit","item");
	}
	
	/**
	 * 派工到员工
	 * Enter description here ...
	 */
	function SetStaffAction(){
		$p["uid"]=$this->user["id"];
		$this->data["StaffInfo"]=CommonModel::getInstance()->getStaffInfo($p);
		$this->view("business.setstaff.edit","item");
	}
	
	/**
	 * 派工驳回
	 * Enter description here ...
	 */
	function SetReturnAction(){
		$p["uid"]=$this->user["id"];
		$this->data["StaffInfo"]=CommonModel::getInstance()->getStaffInfo($p);
		$this->view("business.setreturn.edit","item");
	}
	
	/**
	 * 派工到部门
	 * Enter description here ...
	 */
	function GotoAgencyAction(){
		$p['id'] = PA(P("data",array()),"id","0");
		$p['dis_agency_id'] = PA(P("data",array()),"agency","0");
		$p['dis_staff_id'] = PA(P("data",array()),"staff","0");
		$p['plan_start'] = PA(P("data",array()),"planstart","");
		$p['plan_end'] = PA(P("data",array()),"planend","");
		$p['con_agency_id'] = PA(P("data",array()),"conagency","0");
		$p['plan_work_time'] = PA(P("data",array()),"planwork","0");
		$p['danwei'] = PA(P("data",array()),"danwei","1");
		$p['remark'] = PA(P("data",array()),"remark","");

		$p['plan_work_time'] = $p['plan_work_time'] * $p['danwei'];
		$p['plan_start']=DateTool::intToDateTime(strtotime($p['plan_start'])-3600*8);
		$p['plan_end']=DateTool::intToDateTime(strtotime($p['plan_end'])-3600*8);

		//派工
		BusinessModel::getInstance()->BusinessAgency($p);
		
		//业务进度日志
		$agency = AgencyModel::getInstance()->AgencyInfo($p['con_agency_id']);
		
		$param[2]=$agency["name"];
		$this->BLog(BLogType::AGENCY, $agency["contact"],$p['id']);
		$this->BSLog(BLogType::AGENCY, 0,$p['id'],$param);
		
		$this->json->code("OK")->out();
	}
	
	/**
	 * 派工给员工
	 * Enter description here ...
	 */
	function GotoStaffAction(){
		$p['id'] = PA(P("data",array()),"id","0");
		$p['con_staff_id'] = PA(P("data",array()),"staff","0");
		$p['remark'] = PA(P("data",array()),"remark","0");

		//派工给员工
		BusinessModel::getInstance()->BusinessStaff($p);
		
		//业务进度日志
		$staff = StaffModel::getInstance()->getStaffByName($p['con_staff_id']);
		$param[3]=$staff;
		$this->BLog(BLogType::STAFF, $p['con_staff_id'],$p['id']);
		$this->BSLog(BLogType::STAFF,0,$p['id'],$param);
		
		$this->json->code("OK")->out();
	}
	
	/**
	 * 派工驳回
	 * Enter description here ...
	 */
	function GotoReturnAction(){
		$p['id'] = PA(P("data",array()),"id","0");
		$p['return_staff'] = PA(P("data",array()),"return_staff","");
		$p['return_content'] = PA(P("data",array()),"return_content","");

		//派工驳回
		BusinessModel::getInstance()->BusinessReturn($p);
		
		//业务进度日志
		$agency=0;
		$business = BusinessModel::getInstance()->getBusinessInfo($p);
		if(isset($business["statuss"]) && $business["statuss"] != 0){
			if($business["statuss"] == 1){
				$agency = $business["dis_agency_id"];
			}
			if($business["statuss"] == 2){
				$agency = $business["con_agency_id"];
			}
		}
		$pagency = AgencyModel::getInstance()->AgencyInfo($agency);
		$this->BLog(BLogType::REDIS, $pagency['contact'],$p['id']);
		$this->BSLog(BLogType::REDIS, 0,$p['id']);
		$this->json->code("OK")->out();
	}
	
	/**
	 * 派工驳回
	 * Enter description here ...
	 */
	function RemoveAction(){
		BusinessModel::getInstance()->RemoveReturn();
		$this->json->code("OK")->out();
	}
	
	/**
	 * 历史工单
	 * Enter description here ...
	 */
	function historyAction(){
		$this->view("business.history","item");
	}
	
	/**
	 * 历史工单列表
	 * Enter description here ...
	 */
	function historyListAction(){
		$p["p"] = P("pageIndex",0);
		$p["r"] = P("pageSize",20);
		$p["sortField"] = P("sortField","id");
		$p["sortOrder"] = P("sortOrder","asc");
		$p['types']=P("types","0");
		$p['dis_agency_id']=P("dis_agency_id",$this->user["agency_id"]);
		$p['con_agency_id']=P("con_agency_id",$this->user["agency_id"]);
		$p['created_start']=P("created_start","");
		$p['created_end']=P("created_end","");
		$p['order_no']=P("order_no","0");
		$p['order_id']=P("order_id","");
		$p['statuss']=P("statuss","9");
		$agency = CommonModel::getInstance()->getAgencyAllList();
		$p['dis_agency_id'] = ArrayTool::ArrayToIDs($agency,$p['dis_agency_id']);
		$p['con_agency_id'] = ArrayTool::ArrayToIDs($agency,$p['con_agency_id']);
		$data = BusinessModel::getInstance()->getDispatchingBakList($p);
		$this->json->toJson(array("total"=>BusinessModel::getInstance()->getBakCount($p),"data"=>$data));
	}
	
	/**
	 * 被驳回工单
	 * Enter description here ...
	 */
	function ReturnAction(){
		$this->view("business.return","item");
	}
	
	/**
	 * 被驳回工单列表
	 * Enter description here ...
	 */
	function ReturnListAction(){
		$p["p"] = P("pageIndex",0);
		$p["r"] = P("pageSize",20);
		$p["sortField"] = P("sortField","id");
		$p["sortOrder"] = P("sortOrder","asc");
		$p['types']=P("types","0");
		$p['dis_agency_id']=P("dis_agency_id",$this->user["agency_id"]);
		$p['con_agency_id']=P("con_agency_id",$this->user["agency_id"]);
		$p['created_start']=P("created_start","");
		$p['created_end']=P("created_end","");
		$p['order_no']=P("order_no","0");
		$p['order_id']=P("order_id","");
		$p['statuss']=P("statuss","0");
		$agency = CommonModel::getInstance()->getAgencyAllList();
		$p['dis_agency_id'] = ArrayTool::ArrayToIDs($agency,$p['dis_agency_id']);
		$p['con_agency_id'] = ArrayTool::ArrayToIDs($agency,$p['con_agency_id']);
		$data = BusinessModel::getInstance()->getReturnList($p);
		$this->json->toJson(array("total"=>BusinessModel::getInstance()->getReturnCount($p),"data"=>$data));
	}
	
	/**
	 * 作废工单
	 * Enter description here ...
	 */
	function StopAction(){
		$this->view("business.stop","item");
	}
	
	/**
	 * 作废列表
	 * Enter description here ...
	 */
	function StopListAction(){
		$p["p"] = P("pageIndex",0);
		$p["r"] = P("pageSize",20);
		$p["sortField"] = P("sortField","id");
		$p["sortOrder"] = P("sortOrder","asc");
		$p['types']=P("types","0");
		$p['dis_agency_id']=P("dis_agency_id",$this->user["agency_id"]);
		$p['con_agency_id']=P("con_agency_id",$this->user["agency_id"]);
		$p['created_start']=P("created_start","");
		$p['created_end']=P("created_end","");
		$p['order_no']=P("order_no","0");
		$p['order_id']=P("order_id","");
		$p['statuss']=P("statuss","0");
		$agency = CommonModel::getInstance()->getAgencyAllList();
		$p['dis_agency_id'] = ArrayTool::ArrayToIDs($agency,$p['dis_agency_id']);
		$p['con_agency_id'] = ArrayTool::ArrayToIDs($agency,$p['con_agency_id']);
		$data = BusinessModel::getInstance()->getStopList($p);
		$this->json->toJson(array("total"=>BusinessModel::getInstance()->getStopCount($p),"data"=>$data));
	}
	
	/**
	 * 工单作废
	 * Enter description here ...
	 */
	function ToStopAction(){
		$p['id'] = P("id","0");
		$p['stop'] = 1;
		
		//业务进度日志
		$disagency=P("disagency","0");
		$agency = AgencyModel::getInstance()->AgencyInfo($disagency);
		$rs = BusinessModel::getInstance()->ToStop($p);
		//$business = BusinessModel::getInstance()->getBusinessInfo($p);
		if($rs){
			$param[1]=$p['id'];
			$this->BLog(BLogType::NG, $agency["contact"],$p['id']);
			$this->BSLog(BLogType::NG, 0,$p['id'],$param);
		}
		$this->json->code("OK")->out();
	}
	
	/**
	 * 工单作废
	 * Enter description here ...
	 */
	function ToStartAction(){
		$p['id'] = P("id","0");
		$p['stop'] = 0;
		
		//业务进度日志
		$disagency=P("disagency","0");
		$agency = AgencyModel::getInstance()->AgencyInfo($disagency);
		$rs = BusinessModel::getInstance()->ToStop($p);
		if($rs){
			$param[1]=$p['id'];
			$this->BLog(BLogType::RENG, $agency["contact"],$p['id']);
			$this->BSLog(BLogType::RENG, 0,$p['id'],$param);
		}
		$this->json->code("OK")->out();
	}
	
	/**
	 * 驳回审核通过
	 * Enter description here ...
	 */
	function ReturnOkAction(){
		$p['id'] = P("id","0");
		BusinessModel::getInstance()->ReturnOk($p);
		$this->json->code("OK")->out();
	}
	
	/**
	 * 驳回审核未通过
	 * Enter description here ...
	 */
	function ReturnNgAction(){
		$p['id'] = P("id","0");
		BusinessModel::getInstance()->ReturnNg($p);
		$this->json->code("OK")->out();
	}
	
	// 业务申报表
	function ReceiveAction(){
		$this->view("business.receive","item");
	}
	
	/**
	 * 获取工单配件记录
	 * Enter description here ...
	 */
	function businessDeviceAction(){
		$p["cid"] = P("cid","0");
		$data = DeviceModel::getInstance()->businessDeviceList($p);
		$this->json->toJson(array("total"=>DeviceModel::getInstance()->businessDeviceCount($p),"data"=>$data));
	}
	
	/**
	 * 领取派工设备
	 * Enter description here ...
	 */
	function businessGetAction(){
		$this->view("business.receive.edit","item");
	}
	
	/**
	 * 领取派工设备
	 * Enter description here ...
	 */
	function businessSetAction(){
		$this->view("business.receive.edit","item");
	}
	
	/**
	 * 配件编辑
	 * Enter description here ...
	 */
	function receiveEditAction(){
		$p['id'] = PA(P("data",array()),"id","0");
		$p['staff'] = PA(P("data",array()),"staff","0");
		$p['no'] = PA(P("data",array()),"no","0");
		$p['device'] = PA(P("data",array()),"device","0");
		$types = PA(P("data",array()),"types","");
		$p['types'] = $types;
		$p['count'] = PA(P("data",array()),"count","0");
		$p['remark'] = PA(P("data",array()),"remark","");
		$device = P("name","");
		if($p['types']!="" && $p['device']!=0 && $p['id']!=0){
			$rs = DeviceModel::getInstance()->businessDeviceEdit($p);
			if($rs){
				$name = StaffModel::getInstance()->getStaffByName($p['staff']);
				$param[3]=$name;
				$param[7]=$p['no'];
				$param[8]=$device;
				
				//$this->BLog(BLogType::NG, $agency["contact"],$p['id']);
				if($types == '领取' ){
					$this->BSLog(BLogType::RECEIVE, 0,$p['id'],$param);
				}else{
					$this->BSLog(BLogType::RETURNS, 0,$p['id'],$param);
				}
			}
		}
		$this->json->code("OK")->out();
	}
	
	// 进度汇报
	function ScheduleAction(){
		$this->view("business.schedule","item");
	}
	
	/**
	 * 进度汇报列表
	 * Enter description here ...
	 */
	function ScheduleListAction(){
		$p["p"] = P("pageIndex",0);
		$p["r"] = P("pageSize",20);
		$p["sortField"] = P("sortField","id");
		$p["sortOrder"] = P("sortOrder","asc");
		$p['id']=P("id","0");
		$p['order_id']=P("order_id","");
		$p['types']=P("types","0");
		$p['con_agency_id']=P("con_agency_id",$this->user["agency_id"]);
		$agency = CommonModel::getInstance()->getAgencyAllList();
		$p['con_agency_id'] = ArrayTool::ArrayToIDs($agency,$p['con_agency_id']);
		$data = BusinessModel::getInstance()->getScheduleList($p);
		$this->json->toJson(array("total"=>BusinessModel::getInstance()->getScheduleCount($p),"data"=>$data));
	}
	
	/**
	 * 进度汇报列表详情
	 * Enter description here ...
	 */
	function ScheduleInfoListAction(){
		$p["p"] = P("pageIndex",0);
		$p["r"] = P("pageSize",20);
		$p["sortField"] = P("sortField","id");
		$p["sortOrder"] = P("sortOrder","asc");
		$p["id"] = P("id","0");
		$data = BusinessModel::getInstance()->getScheduleInfoList($p);
		$this->json->toJson(array("total"=>BusinessModel::getInstance()->getScheduleInfoCount($p),"data"=>$data));
	}
	
	/**
	 * 增加进度记录
	 * Enter description here ...
	 */
	function BusinessScheduleAction(){
		$this->view("business.schedule.edit","item");
	}	
	
	/**
	 * 增加进度保存
	 * Enter description here ...
	 */
	function BusinessScheduleEditAction(){
		$p['business_id'] = PA(P("data",array()),"id","0");
		$p['con_agency'] = PA(P("data",array()),"con_agency","0");
		$p['staff_id'] = PA(P("data",array()),"staff_id","0");
		$p['schedule'] = PA(P("data",array()),"schedule","0");
		$p['content'] = PA(P("data",array()),"remark","");
		if($p['business_id']!="" && $p['staff_id']!=0){
			$agency = CommonModel::getInstance()->getAgencyAllList();
			$agency_array = ArrayTool::ArrayToParentIDs($agency,$p["con_agency"]);
			$rs = BusinessModel::getInstance()->BusinessScheduleEdit($p,$agency_array);
			
			if($rs){
				if($p['schedule'] < 100){
					$param[4]=$p['schedule'];
					$param[9]=$p['content'];
					
					$this->BSLog(BLogType::SCHEDULE, 0,$p['business_id'],$param);
				}else{
					$param = array();
					//V($agency_array);
					$agency = implode(',',$agency_array);
					//select group_concat(contact) as aa from crm_contact_view where id in (1,2,3)
					//$this->BLog(BLogType::WORKOK, $agency["contact"],$p['id']);
					$contact = BusinessModel::getInstance()->ContactView($agency);
					//V($contact);
					$this->BLog(BLogType::WORKOK, $contact,$p['business_id']);
					$this->BSLog(BLogType::WORKOK, 0,$p['business_id'],$param);
				}
			}
		}
		$this->json->code("OK")->out();
	}
	
	// 进度确认
	function OkAction(){
		$this->JS["id"]=$this->user["cid"];
		$this->view("business.ok","item");
	}	
	
	/**
	 * 审核画面
	 * Enter description here ...
	 */
	function OkListAction(){
		$id = P("id","0");
		$rs = BusinessModel::getInstance()->BusinessOkList($id);
		$this->json->toJson($rs);
	}
	
	/**
	 * 审核画面
	 * Enter description here ...
	 */
	function ReviewAction(){
		$p["uid"]=$this->user["id"];
		$this->data["StaffInfo"]=CommonModel::getInstance()->getStaffInfo($p);
		$this->view("business.review.edit","item");
	}
	
	/**
	 * 审核数据修改
	 * Enter description here ...
	 */
	function ReviewUpdAction(){
		$p['id'] = PA(P("data",array()),"id","0");
		$p['staff'] = PA(P("data",array()),"staff","");
		$p['status'] = PA(P("data",array()),"status","未审核");
		$p['remark'] = PA(P("data",array()),"remark","");
		//V($p);
		if($p['id'] != "0" && $p['staff'] !=""){
			$business_id = BusinessModel::getInstance()->ReviewBusiness($p);
			//V($business_id);
			$rs = BusinessModel::getInstance()->ReviewUpd($p,$business_id);
			//V($rs);
			$p2["id"]=$business_id;
			$businessInfo = BusinessModel::getInstance()->getBusinessInfo($p2);
			
			//V($businessInfo);
			if($rs){
				$param[3]=$this->user["cname"];
				$this->BLog(BLogType::AUDIT, $businessInfo["con_staff_id"],$business_id);
				$this->BSLog(BLogType::AUDIT, 0,$business_id,$param);
				$param = array();
				$this->BLog(BLogType::OK, $businessInfo["con_staff_id"],$business_id);
				$this->BSLog(BLogType::OK, 0,$business_id,$param);
			}else{
				if($rs && $p['status']=='通过'){
					$param[3]=$this->user["name"];
					$this->BLog(BLogType::AUDIT, $businessInfo["con_staff_id"],$p['id']);
					$this->BSLog(BLogType::AUDIT, 0,$business_id,$param);
				}else{
					$param[3]=$this->user["name"];
					$this->BLog(BLogType::NOTAUDIT, $businessInfo["con_staff_id"],$p['id']);
				}
			}
		}
	}

	// 进度记录
	function TrackAction(){
		$this->view("business.track","item");
	}
	
	// 进度记录
	function TrackListAction(){
		$p["p"] = P("pageIndex",0);
		$p["r"] = P("pageSize",20);
		$p["sortField"] = P("sortField","id");
		$p["sortOrder"] = P("sortOrder","asc");
		$p['id']=P("id","0");
		$p['types']=P("types","0");
		$p['order_id']=P("order_id","");
		$data = BusinessModel::getInstance()->getDispatchingList($p);
		$this->json->toJson(array("total"=>BusinessModel::getInstance()->getCount($p),"data"=>$data));
	}
	
	// 编辑备用编号
	function SetOrderShowAction(){
		$this->view("basic.order.edit","item");
	}
	
	/**
	 * 派工驳回
	 * Enter description here ...
	 */
	function SetOrderAction(){
		$p['id'] = PA(P("data",array()),"id","0");
		$p['order_id'] = PA(P("data",array()),"order_id","");

		//派工驳回
		BusinessModel::getInstance()->SetOrder($p);
		$this->json->code("OK")->out();
	}

    public function AddAction()
    {
        if ($_REQUEST['title']) {
            $types      = P('types','0');
            $title      = P('title');
            $order_id   = P('order_sn');
            $dis_agency_id = P('conagency');
            $customs_id = P('customs_id1');
            $content    = P('content');
            $data = array(
                'title'             => $title,
                'order_id'          => $order_id,
                'content'           => $content,
                'types'             => $types,
                'customer_id'       => $customs_id,
                'dis_agency_id'     => $dis_agency_id,
                'business_staff_id' => $this->user["cid"],
                'business_created'  => time(),
                'created'           => date('Y-m-d H:i:s')
            );
            $business_id = BusinessModel::getInstance()->add($data);

            /*** 向客户发送短信通知 ***/
            $customs = CustomerModel::getInstance()->getCustomerInfo(array('id'=>$customs_id));
            
            $agency = AgencyModel::getInstance()->getContact($types);

            $param[1]=$this->user["name"];
			$this->BLog(BLogType::NEWS, $agency,$business_id);
			$this->BSLog(BLogType::NEWS, 0,$business_id,$param);
					
            //message_send($customs['mobile_phone'], '');
            $this->json->code("OK")->out();exit;
        }
        $this->view("business.add","item");
    }
}